<template>
	<view>
		<!-- 地址 -->
		<navigator url="/pages/user/address/address?source=1" class="address-section">
			<view class="order-content">
				<text class="yticon icon-shouhuodizhi"></text>
				<view class="cen">
					<view class="top">
						<text class="name">{{addressData.ship_name}}</text>
						<text class="mobile">{{addressData.ship_mobile}}</text>
					</view>
					<text class="address">{{addressData.address_detail}}{{addressData.ship_addr}}</text>
				</view>
				<text class="yticon icon-you"></text>
			</view>

			<image class="a-bg" src=""></image>
		</navigator>

		<!-- <block v-for="(shop, index) in shops" :key="shop.shop_id">
			<view class="goods-section">
				<view class="g-header b-b">
					<image class="logo" :src="shop.shop_logo"></image>
					<text class="name">{{shop.shop_name}}</text>
				</view>
				
				<block v-for="(item, index) in shop.goodslist" :key="item.id">
					<view class="g-item">
						<image :src="item.thumb_img"></image>
						<view class="right">
							<text class="title clamp">{{item.goods_name}}</text>
							<text class="spec"> {{item.attr_name}}</text>
							<view class="price-box">
								<text class="price">￥{{item.price}}</text>
								<text class="number">x {{item.goods_number}}</text>
							</view>
						</view>
					</view>
				</block>
			</view>
			<view class="yt-list-cell b-b">
				<view class="cell-icon hb">减</view>
				<text class="cell-tit clamp">商家促销</text>
				<text class="cell-tip active" v-if="shop.shop_coupon.length > 0" @click="toggleMask2('show',index)">{{shop.shop_coupon_name}}</text>
				<text class="cell-tip disabled" v-else>暂无可用优惠</text>
			</view>
		</block> -->
		
		<block >
			<view class="goods-section">
				<view class="g-header b-b">
					<image class="logo" src="##"></image>
					<text class="name">平台自营</text>
				</view>
				<!-- 商品列表 -->
				<block v-for="(item, index) in cartGoods" :key="item.id">
					<view class="g-item">
						<image :src="item.goods.img"></image>
						<view class="right">
							<text class="title clamp">{{item.goods.name}}</text>
							<text class="spec"> {{item.goods.spec_name}}</text>
							<view class="price-box">
								<text class="price">￥{{item.goods.retail_price}}</text>
								<text class="number">x {{item.number}}</text>
							</view>
						</view>
					</view>
				</block>
			</view>
			<!-- <view class="yt-list-cell b-b">
				<view class="cell-icon hb">减</view>
				<text class="cell-tit clamp">商家促销</text>
				<text class="cell-tip active" v-if="shop_coupon.length > 0" @click="toggleMask2('show',index)">{{shop_coupon_name}}</text>
				<text class="cell-tip disabled" v-else>暂无可用优惠</text>
			</view> -->
		</block>
		
		<!-- 优惠明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<view class="cell-icon">券</view>
				<text class="cell-tit clamp">优惠券</text>
				<text class="cell-tip active" v-if="plat_coupon.length > 0" @click="toggleMask('show',)">{{plat_coupon_name}}</text>
				<text class="cell-tip disabled" v-else>暂无可用优惠券</text>
				<text class="cell-more wanjia wanjia-gengduo-d"></text>
			</view>
		</view>
		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">商品金额</text>
				<text class="cell-tip">￥{{goods_fee}}</text>
			</view>
			<view class="yt-list-cell b-b" >
				<text class="cell-tit clamp">优惠金额</text>
				<text class="cell-tip red">-￥{{all_discount}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">运费</text>
				<!-- <text class="cell-tip">￥{{shipping_fee}}</text> -->
				<text class="cell-tip">平台包邮</text>
			</view>
			<view class="yt-list-cell desc-cell">
				<text class="cell-tit clamp">备注</text>
				<input class="desc" type="text" v-model="desc" placeholder="请填写备注信息" placeholder-class="placeholder" />
			</view>
			<!-- <view class="yt-list-cell b-b" >
				<view class="cell-tit clamp">余额({{amount}})<switch :checked="is_use_amount" color="#fa436a" @click="useamount"/></view>
				<text class="cell-tip red">-￥{{use_amount}}</text>
			</view> -->
		</view>
		
		<!-- 底部 -->
		<view class="footer">
			<view class="price-content">
				<text>实付款</text>
				<text class="price-tip">￥</text>
				<text class="price">{{amount}}</text>
			</view>
			<text class="submit" @click="submit">提交订单</text>
		</view>
		
		<!-- 平台优惠券面板 -->
		<view class="mask" :class="maskState===0 ? 'none' : maskState===1 ? 'show' : ''" @click="toggleMask">
			<view class="mask-content" @click.stop.prevent="stopPrevent">
				<!-- 优惠券页面，仿mt -->
				<view class="coupon-item" v-for="(item,index) in plat_coupon" :key="index">
					<view class="con"  @click="choosecoupon(item.id,index)">
						<view class="left">
							<text class="title">{{item.name}}</text>
							<text class="time">有效期至{{item.end_time}}</text>
						</view>
						<view class="right">
							<text class="price" v-if="item.free_type == 1">{{item.coupon_price}}</text>
							<text class="price" v-else>{{item.coupon_price}}折</text>
							<text>满{{item.price}}可用</text>
						</view>
						
						<view class="circle l"></view>
						<view class="circle r"></view>
					</view>
					<text class="tips">{{item.goods_type == 1 ? '全平台商品可用':'指定分类可用'}}</text>
				</view>
			</view>
		</view>
		
		<!-- 店铺优惠券面板 -->
		<view class="mask" :class="maskState2===0 ? 'none' : maskState2===1 ? 'show' : ''" @click="toggleMask2">
			<view class="mask-content" @click.stop.prevent="stopPrevent">
				<!-- 优惠券页面，仿mt -->
				<view class="coupon-item" v-for="(item,index) in shop_coupon" :key="index">
					<view class="con"  @click="choosecoupon2(index)">
						<view class="left">
							<text class="title">{{item.name}}</text>
							<text class="time">有效期至{{item.end_time}}</text>
						</view>
						<view class="right">
							<text class="price">{{item.coupon_price}}</text>
							<text>满{{item.price}}可用</text>
						</view>
						
						<view class="circle l"></view>
						<view class="circle r"></view>
					</view>
					<text class="tips">{{item.name}}</text>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex';
	import app from '../../../Json.js'
	export default {
		data() {
			return {
				desc: '', //备注
				payType: 1, //1微信 2支付宝
				addressData: {},
				shops:{},
				shipping_fee:0,
				goods_fee:0,
				old_fee:0,
				cartId:"",
				kkk:"",
				maskState: 0, //优惠券面板显示状态
				maskState2: 0, //优惠券面板显示状态
				plat_coupon:[],
				shop_coupon:[],
				plat_coupon_name:"选择优惠券",
				plat_cid:'',
				shop_cid:[],
				p_discount:0,
				s_discount:0,
				all_discount:0,
				amount:0,
				numbers:0,
				is_use_amount:false,
				use_amount:0,
				cartGoods:[],
				province:app.province,
				city:app.city,
				area:app.area
			}
		},
		computed:{
			...mapState(['hasLogin','userInfo'])
		},
		onLoad(option){
			var that = this
			let cartId = [];
			that.kkk = option.data;
			try{
				cartId = uni.getStorageSync(option.data);
			}catch(e){
				//TODO handle the exception
			}
			
			that.cartId = cartId.join(",");
			let _param = {
				cartId:cartId.join(","),
				ids:cartId,
				token:that.userInfo.token
			};
			that.$api.postUrl(that.$api._api.ApiCartSub, _param ,function(res){
				that.addressData = res.address
				if(typeof(that.addressData.ship_province_id) != 'undefined'){
					that.addressData = that.buildRes(that.addressData)
				}
				
				//that.shops= _data.data.info
				that.shipping_fee = res.distribution_price
				that.old_fee = that.goods_fee = res.goods_money
				that.plat_coupon = res.coupon
				that.amount = res.money
				that.cartGoods = res.carts
				res.carts.map(re=>{
					that.numbers += re.number
				})
				
				if(that.plat_coupon.length){
					that.choosecoupon(that.plat_coupon[0]['id'], 0)
				}
				
			})
		},
		methods: {
			...mapMutations(["login"]),
			buildRes(re){
				let province_new = "";
				this.province.map(item=>{
					if(item.region_id == re.ship_province_id){
						province_new = item.region_name
					} 
				})
				
				let city_new = "";
				this.city[re.ship_province_id].map(item=>{
					if(item.region_id == re.ship_city_id){
						city_new = item.region_name
					}
				})
				let area_new = "";
				 this.area[re.ship_city_id].map(item=>{
					 if(item.region_id == re.ship_county_id){
					 	area_new = item.region_name
					 }
				})
				
				re.address_detail = province_new + city_new  + area_new;
				return re;
			},
			//显示优惠券面板
			toggleMask(type){
				var that = this
				let timer = type === 'show' ? 10 : 300;
				let	state = type === 'show' ? 1 : 0;
				that.maskState = 2;
				setTimeout(()=>{
					that.maskState = state;
				}, timer)
			},
			toggleMask2(type,index){
				var that = this
				if(type === 'show'){
					that.shop_coupon = that.shops[index].shop_coupon
				}
				let timer = type === 'show' ? 10 : 300;
				let	state = type === 'show' ? 1 : 0;
				that.maskState2 = 2;
				setTimeout(()=>{
					that.maskState2 = state;
				}, timer)
			},
			numberChange(data) {
				this.number = data.number;
			},
			changePayType(type){
				this.payType = type;
			},
			useamount(){
				this.is_use_amount = !this.is_use_amount
				this.all_discount = Number(parseInt(this.p_discount) + parseInt(this.s_discount))
				var order_amount = this.old_fee - this.all_discount
				if(this.is_use_amount){
					if(this.amount >= order_amount){
						this.use_amount = order_amount
						this.goods_fee = 0
					}else{
						this.use_amount = this.amount
						this.goods_fee = order_amount - this.amount
					}
				}else{
					this.use_amount = 0
					this.goods_fee = order_amount
				}
			},
			submit(){
				var that = this
				if(!that.addressData.id){
					that.$api.msg('请选择收货地址');
					return false
				}
				that.$api.loading(true, "订单生成中");
				let _param = {
					cartId:that.cartId.split(","),
					token:that.userInfo.token,
					address_id:that.addressData.id,
					desc:that.desc,
					coupon_receive_id:that.plat_cid,
				}
				that.$api.postUrl(that.$api._api.ApiOrderCreate, _param ,function(res){
					that.$api.loading(false);
					
					let _info = that.userInfo;
					let _num = Math.max(_info.number - that.numbers, 0);
					that.$set(_info, "cart_number", _num);
					that.login(_info);
					that.$api.setCartNumber();
					
					if(typeof(res.pay_type) !== "undefined" && res.pay_type == 3){
						uni.showModal({
							title:'确定使用余额支付？',
							confirmText:'确定',
							success(r) {
								if(r.confirm){
									that.$api.loading(true, "余额支付中...");
									that.$api.postUrl(that.$api._api.ApiOrderAmountBuy,{order_sn:_data.data.order_sn},function(rr){
										that.$api.loading(false);
										let _data1 = {};
										if(typeof(rr.data) == "object"){
											_data1 = rr.data
										}else{
											_data1 = JSON.parse(rr.data)
										}
										that.$api.msg(_data1.message)
										if(_data1.status == 1){
											setTimeout(()=>{
												uni.navigateTo({
													url:'./order'
												})
											},1500)
										}
									})
								}
							}
						})
					}else{
						uni.redirectTo({
							url: '/pages/user/money/pay?oid='+res.oid
						})
					}
					try{
						uni.removeStorage(that.kkk)
					}catch(e){
						//TODO handle the exception
					}
				})
			},
			stopPrevent(){},
			choosecoupon:function(coupon_id,index){
				var that = this
				that.plat_cid = coupon_id
				var amount = this.plat_coupon[index].coupon_price
				var use_con = this.plat_coupon[index].price
				this.p_discount = this.plat_coupon[index].discount_money
				this.all_discount = Number(parseFloat(this.p_discount) + parseInt(this.s_discount))
				if(this.plat_coupon[index].free_type == 1){
					this.plat_coupon_name = '满'+use_con+'减'+amount
				}else{
					this.plat_coupon_name = '满'+use_con+'打'+amount+'折'
				}
				//that.goods_fee = that.old_fee - that.all_discount
				that.toggleMask('hide')
			},
			choosecoupon2:function(index){
				var that = this
				var shop_cid = that.shop_cid
				for(var i=0;i<shop_cid.length;i++){
					if(shop_cid[i].shop_id == that.shop_coupon[index].shop_id){
						that.shop_cid.splice(i,1)
					}
				}
				let obj = {
					cid:that.shop_coupon[index].id,
					shop_id:that.shop_coupon[index].shop_id,
					amount:that.shop_coupon[index].amount,
				}
				that.shop_cid.push(obj)
				var amount = that.shop_coupon[index].amount
				var use_con = that.shop_coupon[index].use_con
				
				let s_discount = 0
				that.shop_cid.forEach(item=>{
					s_discount = Number(parseInt(s_discount) + parseInt(item.amount))
				})
				that.s_discount = s_discount
				that.all_discount = Number(parseInt(that.p_discount) + parseInt(that.s_discount))
			
				var _shops = that.shops
				for(var j=0;j<_shops.length;j++){
					if(_shops[j].shop_id == that.shop_coupon[index].shop_id){
						that.shops[j].shop_coupon_name = '满' + use_con + '减' + amount
					}
				}
				that.goods_fee = that.old_fee - that.all_discount
				that.toggleMask2('hide')
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100rpx;
	}

	.address-section {
		padding: 30rpx 0;
		background: #fff;
		position: relative;

		.order-content {
			display: flex;
			align-items: center;
		}

		.icon-shouhuodizhi {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90rpx;
			color: #888;
			font-size: 44rpx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28rpx;
			color: $font-color-dark;
		}

		.name {
			font-size: 34rpx;
			margin-right: 24rpx;
		}

		.address {
			margin-top: 16rpx;
			margin-right: 20rpx;
			color: $font-color-light;
		}

		.icon-you {
			font-size: 32rpx;
			color: $font-color-light;
			margin-right: 30rpx;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5rpx;
		}
	}

	.goods-section {
		margin-top: 16rpx;
		background: #fff;
		padding-bottom: 1px;

		.g-header {
			display: flex;
			align-items: center;
			height: 84rpx;
			padding: 0 30rpx;
			position: relative;
		}

		.logo {
			display: block;
			width: 50rpx;
			height: 50rpx;
			border-radius: 100px;
		}

		.name {
			font-size: 30rpx;
			color: $font-color-base;
			margin-left: 24rpx;
		}

		.g-item {
			display: flex;
			margin: 20rpx 30rpx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140rpx;
				height: 140rpx;
				border-radius: 4rpx;
			}

			.right {
				flex: 1;
				padding-left: 24rpx;
				overflow: hidden;
			}

			.title {
				font-size: 30rpx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26rpx;
				color: $font-color-light;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32rpx;
				color: $font-color-dark;
				padding-top: 10rpx;

				.price {
					margin-bottom: 4rpx;
				}
				.number{
					font-size: 26rpx;
					color: $font-color-base;
					margin-left: 20rpx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}
	.yt-list {
		margin-top: 16rpx;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 10rpx 30rpx 10rpx 40rpx;
		line-height: 70rpx;
		position: relative;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30rpx;
		}

		.cell-icon {
			height: 32rpx;
			width: 32rpx;
			font-size: 22rpx;
			color: #fff;
			text-align: center;
			line-height: 32rpx;
			background: #f85e52;
			border-radius: 4rpx;
			margin-right: 12rpx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}

		}

		.cell-more {
			align-self: center;
			font-size: 24rpx;
			color: $font-color-light;
			margin-left: 8rpx;
			margin-right: -10rpx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26rpx;
			color: $font-color-light;
			margin-right: 10rpx;
		}

		.cell-tip {
			font-size: 26rpx;
			color: $font-color-dark;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}
			&.red{
				color: $base-color;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90rpx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}
	
	/* 支付列表 */
	.pay-list{
		padding-left: 40rpx;
		margin-top: 16rpx;
		background: #fff;
		.pay-item{
			display: flex;
			align-items: center;
			padding-right: 20rpx;
			line-height: 1;
			height: 110rpx;	
			position: relative;
		}
		.icon-weixinzhifu{
			width: 80rpx;
			font-size: 40rpx;
			color: #6BCC03;
		}
		.icon-alipay{
			width: 80rpx;
			font-size: 40rpx;
			color: #06B4FD;
		}
		.icon-xuanzhong2{
			display: flex;
			align-items: center;
			justify-content: center;
			width: 60rpx;
			height: 60rpx;
			font-size: 40rpx;
			color: $base-color;
		}
		.tit{
			font-size: 32rpx;
			color: $font-color-dark;
			flex: 1;
		}
	}
	
	.footer{
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 995;
		display: flex;
		align-items: center;
		width: 100%;
		height: 90rpx;
		justify-content: space-between;
		font-size: 30rpx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		box-shadow: 0 -1px 5px rgba(0,0,0,.1);
		.price-content{
			padding-left: 30rpx;
		}
		.price-tip{
			color: $base-color;
			margin-left: 8rpx;
		}
		.price{
			font-size: 36rpx;
			color: $base-color;
		}
		.submit{
			display:flex;
			align-items:center;
			justify-content: center;
			width: 280rpx;
			height: 100%;
			color: #fff;
			font-size: 32rpx;
			background-color: $base-color;
		}
	}
	
	/* 优惠券面板 */
	.mask{
		display: flex;
		align-items: flex-end;
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0,0,0,0);
		z-index: 9995;
		transition: .3s;
		
		.mask-content{
			width: 100%;
			min-height: 30vh;
			max-height: 70vh;
			background: #f3f3f3;
			transform: translateY(100%);
			transition: .3s;
			overflow-y:scroll;
		}
		&.none{
			display: none;
		}
		&.show{
			background: rgba(0,0,0,.4);
			
			.mask-content{
				transform: translateY(0);
			}
		}
	}

	/* 优惠券列表 */
	.coupon-item{
		display: flex;
		flex-direction: column;
		margin: 20rpx 24rpx;
		background: #fff;
		.con{
			display: flex;
			align-items: center;
			position: relative;
			height: 120rpx;
			padding: 0 30rpx;
			&:after{
				position: absolute;
				left: 0;
				bottom: 0;
				content: '';
				width: 100%;
				height: 0;
				border-bottom: 1px dashed #f3f3f3;
				transform: scaleY(50%);
			}
		}
		.left{
			display: flex;
			flex-direction: column;
			justify-content: center;
			flex: 1;
			overflow: hidden;
			height: 100rpx;
		}
		.title{
			font-size: 32rpx;
			color: $font-color-dark;
			margin-bottom: 10rpx;
		}
		.time{
			font-size: 24rpx;
			color: $font-color-light;
		}
		.right{
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 26rpx;
			color: $font-color-base;
			height: 100rpx;
		}
		.price{
			font-size: 44rpx;
			color: $base-color;
			&:before{
				content: '￥';
				font-size: 34rpx;
			}
		}
		.tips{
			font-size: 24rpx;
			color: $font-color-light;
			line-height: 60rpx;
			padding-left: 30rpx;
		}
		.circle{
			position: absolute;
			left: -6rpx;
			bottom: -10rpx;
			z-index: 10;
			width: 20rpx;
			height: 20rpx;
			background: #f3f3f3;
			border-radius: 100px;
			&.r{
				left: auto;
				right: -6rpx;
			}
		}
	}

</style>
